package com.myway.core.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.myway.dto.common.JsonData;

/**
 * Created by yufei on 2017/11/7.
 */
@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        String url = request.getRequestURL().toString();
        ModelAndView mv ;
        String defaultMsg = "System error";
        //这里我们要求项目中所有请求json数据，都是用.json结尾
        if(url.endsWith(".json")){
//                log.error("unknow json exception,url:"+url+":"+e.getMessage().toString());
                JsonData result = JsonData.fail(defaultMsg);
                mv = new ModelAndView("jsonView",result.toMap());
        }else if(url.endsWith("page")){//请球page都使用.page结尾
//            log.error("unknow page exception,url:"+url+":"+e.getMessage().toString());
            JsonData result = JsonData.fail(defaultMsg);
            mv = new ModelAndView("exception",result.toMap());
        }else{
//            log.error("unknow exception,url:"+url+":"+e.getMessage().toString());
            JsonData result = JsonData.fail(defaultMsg);
            mv = new ModelAndView("jsonView",result.toMap());
        }
        return mv;
    }
}
